package com.ins1st.plus.web.menu;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ins1st.plus.api.system.menu.entity.SysMenu;
import com.ins1st.plus.api.system.menu.entity.Ztree;
import com.ins1st.plus.api.system.menu.service.SysMenuApi;
import com.ins1st.plus.base.BaseController;
import com.ins1st.plus.base.ControllerResult;
import com.ins1st.plus.base.ServiceResult;
import com.ins1st.plus.core.annotation.Req4Json;
import com.ins1st.plus.core.annotation.Req4Model;
import com.ins1st.plus.util.TreeFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ins1st-plus
 * @description: 菜单控制层
 * @author: coderSun
 * @create: 2019-09-04 17:17
 **/
@Controller
@RequestMapping(value = "/sys/menu")
public class SysMenuController extends BaseController {

    private static final String VIEW = "pages/sys/menu/";

    @Reference
    SysMenuApi sysMenuApi;

    /**
     * 列表页
     *
     * @return
     */
    @Req4Model(value = "/list")
    public String list() {
        return VIEW + "menu_index.html";
    }

    /**
     * 添加页
     *
     * @return
     */
    @Req4Model(value = "/add")
    public String add() {
        return VIEW + "menu_add.html";
    }

    /**
     * 编辑页
     *
     * @return
     */
    @Req4Model(value = "/edit")
    public String edit(Integer id, Model model) {
        SysMenu sysMenu = sysMenuApi.getById(id);
        if (sysMenu.getpId().equals(0)) {
            sysMenu.setpName("顶级菜单");
        } else {
            SysMenu parent = sysMenuApi.getById(sysMenu.getpId());
            sysMenu.setpName(parent.getName());
        }
        model.addAttribute("menu", sysMenu);
        return VIEW + "menu_edit.html";
    }


    /**
     * 查询集合
     *
     * @return
     */
    @Req4Json(value = "/selectList")
    public Object selectList() throws Exception {
        List<SysMenu> sysMenus = sysMenuApi.list();
        TreeFactory<SysMenu> factory = new TreeFactory<>();
        return ControllerResult.success(sysMenus);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Req4Json(value = "/delete/{id}")
    public Object delete(@PathVariable int id) {
        this.sysMenuApi.delete(id);
        return success();
    }


    /**
     * 查询ztree数据
     *
     * @return
     */
    @Req4Json(value = "/selectZtrees")
    public Object selectZtrees() {
        List<SysMenu> sysMenus = sysMenuApi.list();
        List<Ztree> ztrees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            Ztree ztree = new Ztree();
            ztree.setId(sysMenu.getId());
            ztree.setpId(sysMenu.getpId());
            ztree.setName(sysMenu.getName());
            ztree.setLevel(sysMenu.getLevel());
            if (sysMenu.getpId() == 0) {
                ztree.setParent(true);
            }
            ztrees.add(ztree);
        }
        Ztree ztree = new Ztree();
        ztree.setId(0);
        ztree.setName("顶级菜单");
        ztree.setParent(true);
        ztrees.add(ztree);
        return success(ztrees);
    }

    /**
     * 保存菜单
     *
     * @param sysMenu
     * @return
     */
    @Req4Json(value = "/save")
    public Object save(SysMenu sysMenu) {
        return sysMenuApi.saveMenu(sysMenu);
    }


    /**
     * 更新
     *
     * @param sysMenu
     * @return
     */
    @Req4Json(value = "/update")
    public Object update(SysMenu sysMenu) {
        sysMenuApi.updateById(sysMenu);
        return success();
    }

    /**
     * 根据角色查询菜单
     *
     * @param id
     * @return
     */
    @Req4Json(value = "/queryMenus4Role")
    public Object queryMenus4Role(Integer roleId) {
        ServiceResult sr = this.sysMenuApi.queryMenus4Role(roleId);
        if (sr.isSuccess()) {
            return success(sr.getData());
        }
        return error("失败");
    }
}
